package com.nest4cloud.nest.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nest4cloud.nest.account.entity.AccountTypeEntity;
import com.nest4cloud.nest.account.entity.AccountsEntity;
import com.nest4cloud.nest.account.mapper.AccountsMapper;
import com.nest4cloud.nest.account.service.AccountTypeService;
import com.nest4cloud.nest.account.service.AccountsService;
import com.nest4cloud.nest.account.vo.AccountsVo;
import com.nest4cloud.nest.common.security.service.NestUser;
import com.nest4cloud.nest.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 账户表 服务实现类
 *
 * @author nest
 * @date 2024-11-24 21:37:24
 */
@Service
@RequiredArgsConstructor
public class AccountsServiceImpl extends ServiceImpl<AccountsMapper, AccountsEntity> implements AccountsService {

	private final AccountTypeService accountTypeService;

	@Override
	public boolean saveAccount(AccountsEntity accounts) {
		// 当用户添加账单类型是需要获取到添加用户的id
		NestUser user = SecurityUtils.getUser();
		accounts.setUserId(user.getId());

		// 添加账户类型ID
		return this.save(accounts);
	}

	/**
	 * 分页查询账户列表
	 * @param page 分页实体
	 * @param wrapper 查询对象
	 * @return 分页数据
	 */
	@Override
	public Page<AccountsVo> pageEntity(Page page, LambdaQueryWrapper<AccountsEntity> wrapper) {
		Page pageData = this.page(page, wrapper);
		Page<AccountsVo> pageVo = new Page<>();
		pageVo.setTotal(pageData.getTotal());
		pageVo.setSize(pageData.getSize());
		pageVo.setCurrent(pageData.getCurrent());
		List<AccountsEntity> records = pageData.getRecords();
		List<AccountsVo> collect = records.stream().map(acc -> {
			AccountsVo accountsVo = new AccountsVo();
			accountsVo.setId(Long.valueOf(acc.getId()));
			accountsVo.setAccountName(acc.getAccountName());
			accountsVo.setAccountTypeId(acc.getAccountTypeId());
			// 这里需要查询账户类型名称
			AccountTypeEntity accountTypeEntity = accountTypeService.getAccountTypeById(acc.getAccountTypeId());

			accountsVo.setAccountTypeName(accountTypeEntity.getName());
			accountsVo.setBalance(acc.getBalance());
			return accountsVo;
		}).collect(Collectors.toList());
		pageVo.setRecords(collect);
		return pageVo;
	}

	/**
	 * 查询用户的账户总金额
	 * @param accounts
	 * @return
	 */
	@Override
	public String  getTotalAmount(AccountsEntity accounts) {
		QueryWrapper<AccountsEntity> wrapper = new QueryWrapper<AccountsEntity>().select("SUM(balance) as balance")
				.eq("user_id", SecurityUtils.getUser().getId());
		List<Object> res = this.baseMapper.selectObjs(wrapper);
		String balance = new BigDecimal(0).setScale(2, RoundingMode.HALF_UP).toString();
		if (res != null && !res.isEmpty()) {
			if (res.get(0) == null) {
				// 说明该用户还没有启用资金
				return balance;
			}
			return ((BigDecimal) res.get(0)).setScale(2, RoundingMode.HALF_UP).toString();
		}
		return balance;
	}


	/**
	 * 查询登录用户账户信息
	 *
	 * @return 返回登录用户账户信息
	 */
	@Override
	public List<AccountsVo> getUserAccount() {
		LambdaQueryWrapper<AccountsEntity> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AccountsEntity::getUserId, SecurityUtils.getUser().getId());
		List<AccountsEntity> entities = this.baseMapper.selectList(queryWrapper);
		if (entities != null && !entities.isEmpty()) {
			return entities.stream().map(ele -> {
				AccountsVo accountsVo = new AccountsVo();
				accountsVo.setId(Long.valueOf(ele.getId()));
				accountsVo.setAccountName(ele.getAccountName());
				accountsVo.setAccountTypeName(accountTypeService.getAccountTypeById(ele.getAccountTypeId()).getName());
				return accountsVo;
			}).collect(Collectors.toList());
		}
		return Collections.emptyList();
	}
}
